
$(document).ready(function(){
	
	//删除的ID
	 var id;
	//查询的条件
	 var condition ;
	//开始页数
	 var pageStart;
	//执行全查询
	 query(pageStart);
	//条件查询
	 $("#query").click(function() {
		 
		 condition = $('input[name=condition]').val()
		 $.ajax({
				type:"get",
				url:"../queryById/" + condition,
				async:true,
				dataType:"json",
				success:function(result){
					
					if(result.medicalInstitutions == "" || result.medicalInstitutions == null){
						$.ajax({
							type:"get",
							url:"../queryByName/" + condition,
							async:true,
							dataType:"json",
							success:function(result){
								var msg = "";
							$.each(result.medicalInstitutions,function(key,value){
								msg  += "<tr>"+
			                         "<td>"+value.organizationNumber+"</td>"+
			                         "<td>"+value.organizationName+"</td>"+
			                         "<td>"+value.hospitalLevel+"</td>"+
			                         "<td>"+value.mechanismCategory+"</td>"+
			                         "<td>"+value.postalcode+"</td>"+
			                         "<td>"+value.legalName+"</td>"+
			                         "<td>"+value.legalPhone+"</td>"+
			                         "<td>"+value.linkManName+"</td>"+
			                         "<td>"+value.linkPhone+"</td>"+
			                         "<td>"+value.linkManPhone+"</td>"+
			                         "<td>"+value.address+"</td>"+
			                         "<td>"+value.remark+"</td>"+
			                         "<td>"+
			                         "<button onclick = updateInfo("+value.organizationNumber+")>修改</button>"+
			                         "<button onclick = deleteInfo("+value.organizationNumber+")>删除</button>"+
		                  	     "</td>"+
		                      "</tr>";
							});
							var yeshu ;
							yeshu = "<button class='shouye' onclick=query("+ result.medicalInstitutionsPageInfo.navigateFirstPage+")>首页</button>"+
									"<button class='shangyiye' onclick=query("+ result.medicalInstitutionsPageInfo.prePage+")>上一页</button>"+
									"<span>&nbsp;&nbsp;</span>" +"<span>当前第:"+result.medicalInstitutionsPageInfo.pageNum+"页</span>"+
									"<span>&nbsp;&nbsp;</span>"+"<span>总页数：  "+result.medicalInstitutionsPageInfo.pages+"页</span>"+
									"<button class='xiayiye' onclick=query("+ result.medicalInstitutionsPageInfo.nextPage+")>下一页</button>"+
									"<button class='weiye' onclick=query("+ result.medicalInstitutionsPageInfo.navigateLastPage+")>尾页</button>";
							$("#organization").html(msg);
							$("#page").html(yeshu);
							
							if(result.medicalInstitutionsPageInfo.navigateFirstPage == result.medicalInstitutionsPageInfo.pageNum){
								$(".shangyiye").attr("disabled",true);
								$(".shouye").attr("disabled",true);
							}
							if(result.medicalInstitutionsPageInfo.navigateLastPage == result.medicalInstitutionsPageInfo.pageNum){
								$(".xiayiye").attr("disabled",true);
								$(".weiye").attr("disabled",true);
							}
						},
						erroy:function(){
							alert("链接url失败......");
						}
			 
			 });
					}
					
					var msg = "";
					$.each(result.medicalInstitutions,function(key,value){
						msg  += "<tr>"+
	                         "<td>"+value.organizationNumber+"</td>"+
	                         "<td>"+value.organizationName+"</td>"+
	                         "<td>"+value.hospitalLevel+"</td>"+
	                         "<td>"+value.mechanismCategory+"</td>"+
	                         "<td>"+value.postalcode+"</td>"+
	                         "<td>"+value.legalName+"</td>"+
	                         "<td>"+value.legalPhone+"</td>"+
	                         "<td>"+value.linkManName+"</td>"+
	                         "<td>"+value.linkPhone+"</td>"+
	                         "<td>"+value.linkManPhone+"</td>"+
	                         "<td>"+value.address+"</td>"+
	                         "<td>"+value.remark+"</td>"+
	                         "<td>"+
	                         "<button onclick = updateInfo("+value.organizationNumber+")>修改</button>"+
	                         "<button onclick = deleteInfo("+value.organizationNumber+")>删除</button>"+
                  	     "</td>"+
                      "</tr>";
					});
					var yeshu ;
					yeshu = "<button class='shouye' onclick=query("+ result.medicalInstitutionsPageInfo.navigateFirstPage+")>首页</button>"+
					"<button class='shangyiye' onclick=query("+ result.medicalInstitutionsPageInfo.prePage+")>上一页</button>"+
					"<span>&nbsp;&nbsp;</span>" +"<span>当前第:"+result.medicalInstitutionsPageInfo.pageNum+"页</span>"+
					"<span>&nbsp;&nbsp;</span>"+"<span>总页数：  "+result.medicalInstitutionsPageInfo.pages+"页</span>"+
					"<button class='xiayiye' onclick=query("+ result.medicalInstitutionsPageInfo.nextPage+")>下一页</button>"+
					"<button class='weiye' onclick=query("+ result.medicalInstitutionsPageInfo.navigateLastPage+")>尾页</button>";
					$("#organization").html(msg);
					$("#page").html(yeshu);
					if(result.medicalInstitutionsPageInfo.navigateFirstPage == result.medicalInstitutionsPageInfo.pageNum){
						$(".shangyiye").attr("disabled",true);
						$(".shouye").attr("disabled",true);
					}
					if(result.medicalInstitutionsPageInfo.navigateLastPage == result.medicalInstitutionsPageInfo.pageNum){
						$(".xiayiye").attr("disabled",true);
						$(".weiye").attr("disabled",true);
					}
				},
				erroy:function(){
					alert("链接url失败......");
				}
	 
		 }); 
		 
	 });
	 
	 
})

	//点击新增按钮显示新增项目信息
	 $("#insert").click(function() {
		 //淡入3秒
		 $("#addInfo").fadeIn(3000);
	 });

	//点击添加信息
	$("#append").click(function() {
		var pageStart;
		console.log($("#add").serialize());
		//添加的定点机构信息内容
		 $.ajax({
			    type:"get",
				url:"/Medical/insert?"+$("#add").serialize(),
				async:true,
				success:function(result){
					query(pageStart);
					 console.log("成功");
					$("#addInfo").fadeOut(100);
				},
				erroy:function(){
					alert("失败！！！");
				}
		 });
	 });


function query(pageStart){
	if(pageStart == ""|| pageStart == null){
		pageStart = 1;
	}else{
		pageStart = pageStart
	}
		// 执行全查询
		 $.ajax({
				type:"get",
				url:"../queryAll/" + pageStart,
				async:true,
				dataType:"json",
				success:function(result){
					var msg = "";
					$.each(result.medicalInstitutions,function(key,value){
						msg  += "<tr>"+
	                         "<td>"+value.organizationNumber+"</td>"+
	                         "<td>"+value.organizationName+"</td>"+
	                         "<td>"+value.hospitalLevel+"</td>"+
	                         "<td>"+value.mechanismCategory+"</td>"+
	                         "<td>"+value.postalcode+"</td>"+
	                         "<td>"+value.legalName+"</td>"+
	                         "<td>"+value.legalPhone+"</td>"+
	                         "<td>"+value.linkManName+"</td>"+
	                         "<td>"+value.linkPhone+"</td>"+
	                         "<td>"+value.linkManPhone+"</td>"+
	                         "<td>"+value.address+"</td>"+
	                         "<td>"+value.remark+"</td>"+
	                         "<td>"+
	                          "<button onclick = updateInfo("+value.organizationNumber+")>修改</button>"+
                         	  "<button onclick = deleteInfo("+value.organizationNumber+")>删除</button>"+
                     	     "</td>"+
                         "</tr>";
					});
					var yeshu ;
					yeshu = "<button class='shouye' onclick=query("+ result.medicalInstitutionsPageInfo.navigateFirstPage+")>首页</button>"+
					"<button class='shangyiye' onclick=query("+ result.medicalInstitutionsPageInfo.prePage+")>上一页</button>"+
					"<span>&nbsp;&nbsp;</span>" +"<span>当前第:"+result.medicalInstitutionsPageInfo.pageNum+"页</span>"+
					"<span>&nbsp;&nbsp;</span>"+"<span>总页数：  "+result.medicalInstitutionsPageInfo.pages+"页</span>"+
					"<button class='xiayiye' onclick=query("+ result.medicalInstitutionsPageInfo.nextPage+")>下一页</button>"+
					"<button class='weiye' onclick=query("+ result.medicalInstitutionsPageInfo.navigateLastPage+")>尾页</button>";
					$("#organization").html(msg);
					$("#page").html(yeshu);
					if(result.medicalInstitutionsPageInfo.navigateFirstPage == result.medicalInstitutionsPageInfo.pageNum){
						$(".shangyiye").attr("disabled",true);
						$(".shouye").attr("disabled",true);
					}
					if(result.medicalInstitutionsPageInfo.navigateLastPage == result.medicalInstitutionsPageInfo.pageNum){
						$(".xiayiye").attr("disabled",true);
						$(".weiye").attr("disabled",true);
					}
				},
				erroy:function(){
					alert("链接url失败......");
					
				}
				
	 });
	 
		 
		 
	 
	 }



function deleteInfo(id){
	alert("开始");
	 $.ajax({
			type:"get",
			url:"../delete/" + id,
			async:true,
			dataType:"json",
			success:function(result){
				alert("删除成功");
				query(1);
				
			},
			erroy:function(){
				alert("删除失败");
			}
	 });	 
}
	 
function updateInfo(id){
	alert("更新。。。。。");
	var pageStart;
	$("#updateInfo").fadeIn(3000);
	 $.ajax({
			type:"get",
			url:"../queryById/" + id,
			async:true,
			dataType:"json",
			success:function(result){
				console.log("成功");
				let data = result;
				$.each(result.medicalInstitutions,function (key,value){
					$("input[name='organizationNumber']").val(value.organizationNumber);
					$("input[name='organizationName']").val(value.organizationName);
					$("input[name='hospitalLevel']").val(value.hospitalLevel);
					$("input[name='mechanismCategory']").val(value.mechanismCategory);
					$("input[name='postalcode']").val(value.postalcode);
					$("input[name='legalName']").val(value.legalName);
					$("input[name='legalPhone']").val(value.legalPhone);
					$("input[name='linkManName']").val(value.linkManName);
					$("input[name='linkPhone']").val(value.linkPhone);
					$("input[name='linkManPhone']").val(value.linkManPhone);
					$("input[name='address']").val(value.address);
					$("input[name='remark']").val(value.remark);
				});
				
				//点击提交，更新信息
				$("#updateTo").click(function() {
					
					console.log($("#update").serialize());
					//添加的定点机构信息内容
					 $.ajax({
						    type:"get",
						    url:"/Medical/update?"+$("#update").serialize(),
							async:true,
							success:function(result){
								 query(pageStart);
								 $("#updateInfo").fadeOut(3000);
								 console.log("成功");
							},
							erroy:function(){
								alert("失败！！！");
							}
					 });
				 });
			},
			erroy:function(){
				console.log("失败");
				alert(2222);
			}
	 });	 
}


		    
